package com.wan37.logic.dao.impl;

import com.wan37.logic.dao.SceneDao;
import com.wan37.logic.domain.entity.SceneDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-06-01 19:04
 **/
@Transactional(propagation = Propagation.SUPPORTS, readOnly = false, rollbackFor = Exception.class)
@Repository
public class SceneDaoImpl implements SceneDao {


    @Autowired
    HibernateTemplate hibernateTemplate;


    @Override
    public boolean add(SceneDO sceneDO) {
        try {
            hibernateTemplate.save(sceneDO);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean delete(Integer sceneId) {
        try {
            hibernateTemplate.delete(sceneId);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean update(SceneDO sceneDO) {
        try {
            hibernateTemplate.update(sceneDO);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public SceneDO getSceneBySceneId(String sceneId) {
        List<SceneDO> list = (List<SceneDO>) hibernateTemplate.find("from SceneDO where sceneId = ?", sceneId);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<SceneDO> getAll() {
        return (List<SceneDO>) hibernateTemplate.find("from SceneDO");
    }
}
